package com.manager.book;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.logging.SimpleFormatter;

public class BookManager {
    private Book[] books;
    private Scanner input = new Scanner(System.in);
    public void init(){
        books=new Book[10];
        Book book0=new Book();
        book0.setId(1000);
        book0.setName("java");
        book0.setState(1);
        //book0.setBorrowDate(null);
        //book0.setBorrowCount(0);
        Book book1=new Book();
        book1.setId(1001);
        book1.setName("高并发");
        book1.setState(1);

        Book book2=new Book();
        book2.setId(1002);
        book2.setName("分布式");
        book2.setState(1);

        books[0] = book0;
        books[1] = book1;
        books[2] = book2;

    }
    public void menu(){
        System.out.println("欢迎进入图书管理系统");
        System.out.println("请根据提示选择");
        System.out.println("1.增加图书");
        System.out.println("2.删除图书");
        System.out.println("3.借阅图书");
        System.out.println("4.归还图书");
        System.out.println("5.图书风云榜");
        System.out.println("6.查看图书");
       int choice = input.nextInt();
       switch (choice){
           case 1:
               addBook();
               break;
           case 2:
                deleteBook();
               break;
           case 3:
               borrowBook();
               break;
           case 4:
               returnBook();
               break;
           case 5:
               bookRank();
               break;
           case 6:
               showBook();
           default:
       }
    }

    public void isCountinue(){
        System.out.println("是否继续（y/其他）");
        String isCountinue = input.next();
        if ("y".equals(isCountinue)){
            menu();
        }
    }

    private void addBook(){
        System.out.println("增加");

        Book book = new Book();
        System.out.println("请输入书的编号'");
        int id = input.nextInt();
        System.out.println("请输入书的名字'");
        String name = input.next();

        int state =1 ;
        //Date bDate = null ;
       // int bCount = 0 ;
        book.setId(id);
        book.setName(name);
        book.setState(1);

       // book.setBorrowDate(null);
        int position = -1 ;
        for (int i=0;i<books.length;i++){
            if (books[i] == null){
                position = i ;
                break;
            }
        }
        books[position]=book;
        isCountinue();
    }

    private void deleteBook(){
        System.out.println("删除");
        System.out.println("请输入要删除的书名");
        String name = input.next();
        int position = -1;
        for (int i=0;i<books.length;i++){
            if (books[i].getName().equals(name)){
                position = i ;
                break;
            }
        }
        int start = position+1 ;
        int firstNullPosition=-1;
        for (int i=0;i<books.length;i++){
            if (books[i]==null){
                firstNullPosition = i ;
                break;
            }
        }

        int end = firstNullPosition-1 ;

        if (firstNullPosition==-1){
            end = books.length-1 ;
        }

        for (int i=start;i<=end;i++){
        books[i-1] = books[i] ;
        }
        books[end] = null ;
        isCountinue();
    }

    private void borrowBook(){
        System.out.println("借阅");
        System.out.println("请输入要借阅书的名字");
        String name = input.next();

        int position = -1;
        for (int i=0;i<books.length;i++){
            if (books[i].getName().equals(name)){
                position = i ;
                break;
            }
        }
        books[position].setState(0);
        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String dateStr = sdf.format(date);
        books[position].setBorrowDate(date);

        isCountinue();
    }

    private void returnBook(){
        System.out.println("归还");
        System.out.println("请输入要归还书的名字");
        String name = input.next();

        int position = -1;
        for (int i=0;i<books.length;i++){
            if (books[i].getName().equals(name)){
                position = i ;
                break;
            }
        }
books[position].setState(1);
        books[position].setBorrowDate(null);

       int count = books[position].getBorrowCount() +1;
        books[position].setBorrowCount(count);

        isCountinue();
    }

    private void bookRank(){
        System.out.println("风云榜");

        Book[] newBooks = new Book[books.length] ;

        //对象之间 直接赋值： 赋值的时 内存地址（引用）
        for (int i =0;i<books.length;i++){
            if (books[i]!=null){
                newBooks[i] = books[i];
            }
        }


        for (int i=0;i<newBooks.length-1;i++){
            for (int j=0;j<newBooks.length-1-i;j++){
                if (newBooks[j] !=null && newBooks[j+1] !=null) {
                    if (newBooks[j].getBorrowCount() < newBooks[j + 1].getBorrowCount()) {
                        Book temp = newBooks[j];
                        newBooks[j] = newBooks[j + 1];
                        newBooks[j + 1] = temp;
                    }
                }
            }
        }
//        showBook();
//        isCountinue();
        System.out.println("编号\t\t书名\t\t库存\t\t被借日期\t\t被借次数");
        for (Book book:newBooks) {
            if (book != null) {
                // String borrowStr = book.getBorrowDate()==null ? "-" :book.getBorrowDate()+"";
                String borrowdateStr = "" ;
                if (book.getBorrowDate() == null){
                    borrowdateStr = "-" ;
                }else {
                    borrowdateStr = book.getBorrowDate()+"";
                    Date bDate = book.getBorrowDate();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    borrowdateStr = sdf.format(bDate);
                }
                String stateStr =book.getState()==0?"被借":"在库";
                System.out.println(book.getId() + "\t\t" + book.getName() + "\t\t" + stateStr + "\t\t\t\t" + borrowdateStr+ "\t\t" + book.getBorrowCount());
            }
        }
        isCountinue();
    }

    //查看所有书列表
    private void showBook(){
        System.out.println("编号\t\t书名\t\t库存\t\t被借日期\t\t被借次数");
        for (Book book:books) {
            if (book != null) {
               // String borrowStr = book.getBorrowDate()==null ? "-" :book.getBorrowDate()+"";
                String borrowdateStr = "" ;
                if (book.getBorrowDate() == null){
                    borrowdateStr = "-" ;
                }else {
                    borrowdateStr = book.getBorrowDate()+"";
                    Date bDate = book.getBorrowDate();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    borrowdateStr = sdf.format(bDate);
                }
                String stateStr =book.getState()==0?"被借":"在库";
                System.out.println(book.getId() + "\t\t" + book.getName() + "\t\t" + stateStr + "\t\t\t\t" + borrowdateStr+ "\t\t" + book.getBorrowCount());
            }
        }
        isCountinue();
    }
}
